package com.example.demo.hutool.redis.cluster;

import com.example.demo.hutool.redis.ssl.SocketFactory;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisSlotBasedConnectionHandler;
import redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException;


import java.util.Set;

public class JedisSlotAdvancedConnectionHandler extends JedisSlotBasedConnectionHandler {

    public JedisSlotAdvancedConnectionHandler(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig, int timeout) throws Exception {
        super(nodes,poolConfig,timeout,10000,100000,null,"123456","name",true,SocketFactory.getSocketFactory("ca.crt", "redis.crt", "redis.key", "123456"),null,null,null);


    }

    public JedisPool getJedisPoolFromSlot(int slot){
        JedisPool connectionPool = cache.getSlotPool(slot);


        if(connectionPool != null){
            return connectionPool;
        }else{
            renewSlotCache();
            connectionPool = cache.getSlotPool(slot);
            if(connectionPool != null){
                return connectionPool;
            }else{
                throw new JedisNoReachableClusterNodeException("No reachable node in cluster for slot "+slot);
            }
        }
    }
}
